package chapter01.v2;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {

	private String _name;

	private Vector<Rental> _rentals = new Vector<Rental>();

	public String getName() {
		return _name;
	}

	public void setName(String _name) {
		this._name = _name;
	}

	public Vector<Rental> getRentals() {
		return _rentals;
	}

	public void setRentals(Vector<Rental> _rentals) {
		this._rentals = _rentals;
	}

    /**
     * 生成详单
	 * @return
     */
	public String statement() {
		// 总消费金额
		double totalAmount = 0;
		// 常客积点
		int frequentRenterPoints = 0;

		//所有租赁信息
		Enumeration<Rental> rentals = _rentals.elements();

		String result = "Rental Record for " + getName() + "\n";

		while (rentals.hasMoreElements()) {
			Rental each = (Rental) rentals.nextElement();

			double thisAmount = getAmountFor(each);

			frequentRenterPoints++;

			if ((each.getMovie().getPriceCode() == Movie.NEW_RELEASE) && each.getDaysRented() > 1) {
				frequentRenterPoints++;
			}
			result += "\t" + each.getMovie().getTitle() + "\t" + String.valueOf(thisAmount) + "\n";
			totalAmount += thisAmount;

		}

		result += "Amount owed is " + String.valueOf(totalAmount) + "\n";
		result += "You earned " + String.valueOf(frequentRenterPoints) + " frequent renter points";

		return result;
	}

	private double getAmountFor(Rental aRental) {
		double thisAmount = 0;
		switch (aRental.getMovie().getPriceCode()) {
			case Movie.REGULAR:
				thisAmount += 2;
				if (aRental.getDaysRented() > 2) {
					thisAmount += (aRental.getDaysRented() - 2) * 1.5;
				}
				break;
			case Movie.NEW_RELEASE:
				thisAmount += aRental.getDaysRented() * 3;
				break;
			case Movie.CHILDRENS:
				thisAmount += 1.5;
				if (aRental.getDaysRented() > 3) {
					thisAmount += (aRental.getDaysRented() - 3) * 1.5;
				}
			break;
		}
		return thisAmount;
	}
}
